﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data.Linq.Mapping;
using System.Data.Linq;

#endregion


namespace ESF.General.Entity
{
	[DataContract]
	[Table(Name="esf_sso.TreeBasicReference")]
	[Serializable]
	public class TreeBasicReference : IEquatable<TreeBasicReference>
	{
		[DataMember]
		[Column (IsDbGenerated=true, IsPrimaryKey=true)]
		public int? ID { set; get; }
		

		#region Parent

		[Column]
		public int? ParentBasicReferenceID { set; get; }

		private EntityRef<BasicReference> _ParentBasicReference = new EntityRef<BasicReference> ();

		[Association( Name = "FK_TreeParBasRef_BasRef", IsForeignKey = true, Storage = "_ParentBasicReference", ThisKey = "ParentBasicReferenceID" )]
		public BasicReference Parent { set { _ParentBasicReference.Entity = value; } get { return _ParentBasicReference.Entity; } }

		#endregion

		
		#region Child

		[Column]
		public int? BasicReferenceID { set; get; }

		private EntityRef<BasicReference> _BasicReference = new EntityRef<BasicReference> ();

		[Association( Name = "FK_TreeBasRef_BasRef", IsForeignKey = true, Storage = "_BasicReference", ThisKey = "BasicReferenceID" )]
		public BasicReference Child { set { _BasicReference.Entity = value; } get { return _BasicReference.Entity; } }

		#endregion


		[DataMember]
		[Column]
		public string Value { set; get; }


		#region Funciones

		public bool Equals(TreeBasicReference pOther)
		{
			bool equals;


			equals = false;

			if (pOther == null) { return false; }

			if (ID != null && pOther.ID != null)
			{
				return (ID == pOther.ID);
			}
			else
			{
				if (Parent != null && pOther.Parent != null)
				{
					equals = Parent.Equals (pOther.Parent);
					
					if (Child == null && pOther.Child == null)
					{
						return equals;
					}
					else if (Child != null && pOther.Child != null)
					{
						equals &= Child.Equals (pOther.Child);
					}
				}
			}

			return equals;
		}

		#endregion
	}
}
